'use strict';

Object.defineProperty(exports, "__esModule", {
    value: true
});
exports.promiseQueue = undefined;

var _final = require('./final');

var _final2 = _interopRequireDefault(_final);

var _promiseTimeout = require('./promiseTimeout');

var _promiseTimeout2 = _interopRequireDefault(_promiseTimeout);

var _Deferred = require('./Deferred');

var _Deferred2 = _interopRequireDefault(_Deferred);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var promiseQueue = exports.promiseQueue = function promiseQueue() {
    var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
        _ref$timeout = _ref.timeout,
        timeout = _ref$timeout === undefined ? 5 : _ref$timeout;

    var done = 0;
    var queue = [];

    var deferred = (0, _Deferred2.default)();

    var add = function add(fn) {
        if (checkDone()) {
            throw 'queue已经执行完毕';
        }
        queue.push(Promise.resolve(fn()));
    };
    var checkDone = function checkDone() {
        if (done >= queue.length) {
            return true;
        }
    };
    return deferred;
    var _addPool = function _addPool(fn) {
        var _id = id++;
        doPools.push(_id);
        (0, _final2.default)(fn(), function () {
            var index = doPools.indexOf(_id);
            if (index < -1) {
                return;
            }
            doPools.splice(index, 1);
            runWillPool();
        });
        return _id;
    };
    var runWillPool = function runWillPool() {
        if (doPools.length >= size) {
            return;
        }
        var fn = willPools.shift();
        if (!fn) {
            return;
        }
        addPool(fn);
    };

    return {};
};
exports.default = promiseQueue;